import ButtonShowDialog from "@/views/modules/form/core/dialog/ButtonShowDialog.vue";
import SysAppAllMenu from "@/views/modules/system/core/SysAppAllMenu";
import {Util} from "@lowcode/jpaas-common-lib";
import SysMenuApi from "@/api/system/core/sysMenu";
import ScriptVm from "@/api/form/core/ScriptVm";
import ButtonScript from "@/views/modules/form/core/formPermission/ButtonScript.vue";

export default {
    mixins: [ScriptVm],
    data() {
        return {
            activeKey: 'returnConf',
            bodyScript: '',
            returnCols: [
                {
                    title: this.$ti18('rxList.index', '序号'),
                    dataIndex: 'index',
                    type: 'indexColumn',
                    scopedSlots: {customRender: 'index'}
                },
                {
                    title: this.$ti18('name', '字段名称', 'apiFormCore'),
                    dataIndex: 'header',
                    scopedSlots: {customRender: 'header'}
                },
                {
                    title: this.$ti18('key', '字段KEY', 'apiFormCore'), dataIndex: 'field'}],
                    searchCols: [{
                        title: this.$ti18('paramType', '参数类型', 'apiFormCore'),
                        dataIndex: 'type',
                        displayField: 'type_name',
                        scopedSlots: {customRender: 'type'},
                        width: 120
                },
                {
                    title: this.$ti18('searchField', '搜索字段', 'apiFormCore'),
                    dataIndex: 'fieldName',
                    scopedSlots: {customRender: 'fieldName'},
                    width: 120
                },
                {
                    title: this.$ti18('label', '标签', 'apiFormCore'),
                    dataIndex: 'fieldLabel',
                    scopedSlots: {customRender: 'fieldLabel'},
                    width: 120
                }],
                topBtnsCols: [
                    {
                        title: this.$ti18('buttonName', '按钮名称', 'apiFormCore'),
                        dataIndex: 'btnLabel',
                        scopedSlots: {customRender: 'btnLabel'}
                    },
                    {
                        title: this.$ti18('buttonMark', '按钮标识', 'apiFormCore'),
                        dataIndex: 'btnName',
                        scopedSlots: {customRender: 'btnName'}
                    },
                    {
                        slots: {title: 'eventFunction'},
                        dataIndex: 'btnClick',
                        scopedSlots: {customRender: 'btnClick'}
                    },
                    {
                        title: this.$ti18('buttonIcon', '按钮图标', 'apiFormCore'),
                        dataIndex: 'btnIcon',
                        scopedSlots: {customRender: 'btnIcon'}
                    },
                    {
                        title: this.$ti18('buttonConfig','按钮配置','apiFormCore'),
                        dataIndex: 'config',
                        scopedSlots: {customRender: 'config'}
                    },
                    {
                        title: this.i18n2('displayCon','显示条件'),
                        dataIndex: 'btnShow',
                        scopedSlots: {customRender: 'btnShow'}
                    }
            ],
            modeOptions:[{label: this.$ti18('modeNode',"根节点","apiFormCore"),value:'node'},{label: this.$ti18('modeChildren',"普通节点","apiFormCore"),value:'children'}],
            fieldDatas: [],

            searchDatas: [],
            topBtnsDatas: [],
            defaultBtns:[],
            scriptInsertAry: [],
            fieldsJson:[]

        }
    },
    created() {
        this.scriptInsertAry.push(...this.commonJsLib);
    },
    methods: {
        i18n2(name, text, key) {
            return this.$ti18(name, text, "apiFormCore", key);
        },
        i18n3(name, text, key) {
            return this.$ti18(name, text, "formCellRender", key);
        },
        reloadTopBtnCols(values){
            /*if(values.openNode=='YES' && this.topBtnsCols.length != 5) {
                this.topBtnsCols.push({
                    title: this.$ti18('buttonMode', '适用范围', 'apiFormCore'),
                    dataIndex: 'btnMode',
                    scopedSlots: {customRender: 'btnMode'}
                });
            }*/
        },
        configButton(row) {
            let type = row.btnType;
            let key = "func_" + type;
            if (!this[key]) {
                let msg = this.i18n2('pleaseDefFun1','请定义[') + `${key}` + this.i18n2('pleaseDefFun2',']方法')
                this.$message.warning(msg);
                return;
            }
            this[key](row);
        },
        clearBtnConfig(record){
            let self_=this;
            this.$confirm({
                title: self_.$ti18('rxMsa.tipsInfo','提示信息'),
                content: '确认删除配置吗?',
                onOk() {
                    record.config={};
                    record.isSetedBtn=false;
                },
                onCancel() {},
                okText:this.$ti18('rxForm.ackBtn','确认'),
                cancelText:this.$ti18('rxForm.cancelBtn','取消')
            });
        },
        func_DialogMethod(row){
            var self = this;
            var config = {
                "curVm": self,
                data: {
                    "config": row.config,
                    "btnMode": row.btnMode,
                    "isDefault": row.isDefault
                },
                component: ButtonShowDialog,
                title: this.i18n3('list.listMethod','列表按钮配置'),
                widthHeight: ['600px', '400px']
            };
            Util.open(config, function (action, data) {
                if (action != 'ok') return;
                row.config = data.config;
                row.btnMode = data.btnMode;
                row.isSetedBtn=true
            });
        },
        openShowSetting(record) {
            if (!record.btnShow) {
                record.btnShow = "";
            }
            var config = {
                curVm: this,
                max:true,
                component: ButtonScript,
                data: {
                    pkId: this.pkId,
                    script: record.btnShow,
                    isTreeDialog : true
                },
                title: this.i18n('list.displayConfig','按钮显示条件配置')
            };
            Util.open(config, function (action, data) {
                if (action == "ok") {
                    record.btnShow = data;
                    record.isSetedShowCondition = true
                }

            });
        },
        clearBtnShowCondition(record){
            let self_=this;
            this.$confirm({
                title: self_.$ti18('rxMsa.tipsInfo','提示信息'),
                content: self_.i18n2('sureDelete','确认删除配置吗?'),
                onOk() {
                    record.btnShow="";
                    record.isSetedShowCondition=false;
                },
                onCancel() {},
                okText:this.$ti18('rxForm.ackBtn','确认'),
                cancelText:this.$ti18('rxForm.cancelBtn','取消')
            });
        },
        modeChange(modeValue,row){
            row.mode=modeValue.join(',');
        },
        checkAllFields(e) {
            var table = this.$refs["table_returnConf"];
            var checked = e.target.checked;
            var data = table.getData();
            for (var i = 0; i < data.length; i++) {
                var row = data[i];
                table.updateRow(row, {isReturn: checked});
            }
        },
        addSearchRow() {
            this.addRow({
                type: 'income',
                type_name: this.$ti18('incomPara', '传入参数', 'apiFormCore'),
                fieldOp: 'EQ',
                fieldName: ''
            })
        },
        addToolbarRow() {
            this.addRow({btnLabel: '', btnName: '', btnClick: '', btnIcon: '',needConfig:true,isDefault:false,btnType:"DialogMethod",btnMode:'all',mode:'children'})
        },
        addRow(row) {
            this.$refs["table_" + this.activeKey].addRow(row);
        },
        removeRow() {
            this.$refs["table_" + this.activeKey].removeRows();
        },
        upRow() {
            this.$refs["table_" + this.activeKey].moveUp();
        },
        downRow() {
            this.$refs["table_" + this.activeKey].moveDown();
        },
        topUp(){
            this.$refs["table_" + this.activeKey].moveTopUp();
        },
        bottomUp(){
            this.$refs["table_" + this.activeKey].moveBottomUp();
        },
        addToolbarAllRows() {
            var key = this.mdl.key;
            this.defaultBtns = [
                {
                    btnLabel: this.$ti18('rxList.add', '新增'),
                    btnName: key + 'AddSub',
                    btnIcon: '{"type":"jpaasIcon","icon":"iconjia"}',
                    btnClick: 'onAddSub(btn,tree,data)',
                    isDefault:true,
                    needConfig:true,
                    btnType:"DialogMethod",
                    btnMode:'all',
                    mode:'children'
                },
                {
                    btnLabel: this.$ti18('rxList.editBtn', '编辑'),
                    btnName: key + 'Edit',
                    btnIcon: '{"type":"jpaasIcon","icon":"iconedit"}',
                    btnClick: 'onEdit(btn,tree)',
                    isDefault:true,
                    needConfig:true,
                    btnType:"DialogMethod",
                    btnMode:'children',
                    mode:'children'
                },
                {
                    btnLabel: this.$ti18('rxList.delBtn', '删除'),
                    btnName: key + 'Remove',
                    btnIcon: '{"type":"jpaasIcon","icon":"icontrash"}',
                    btnClick: 'onRemove(btn,tree)',
                    isDefault:true,
                    btnMode:'children',
                    mode:'children'
                },
                {
                    btnLabel: this.$ti18('rxList.openBtn', '展开'),
                    btnName: key + 'RowExpand',
                    btnIcon: '{"type":"jpaasIcon","icon":"icondown"}',
                    btnClick: 'onRowExpand(tree,data,btn)',
                    btnMode:'all',
                    mode:'node,children'
                },
                {
                    btnLabel: this.$ti18('rxList.putAwayBtn', '收起'),
                    btnName: key + 'RowCollapse',
                    btnIcon: '{"type":"jpaasIcon","icon":"iconup"}',
                    btnClick: 'onRowCollapse(tree,data,btn)',
                    btnMode:'all',
                    mode:'node,children'
                },
                {
                    btnLabel: this.$ti18('rxList.refreshBtn', '刷新'),
                    btnName: key + 'RefreshTree',
                    btnIcon: '{"type":"jpaasIcon","icon":"iconVue3_reset"}',
                    btnClick: 'refreshTree()',
                    btnMode:'all',
                    mode:'node,children'
                }];
            if (this.topBtnsDatas.length==0){
                this.topBtnsDatas=this.defaultBtns;
            }
            let btns=this.defaultBtns;
            for(let i=0;i<btns.length;i++){
                let tmpBtns=this.topBtnsDatas.filter(item=>{
                    return btns[i].btnName==item.btnName;
                });
                //如果按钮已经存在直接跳过
                if(tmpBtns.length>0){
                    continue;
                }
                let btn= btns[i];
                btn.idx_=Util.randomId();
                this.topBtnsDatas.push(btn);
            }
        },
        joinToolbarRs() {
            var table = this.$refs["table_" + this.activeKey];
            var btns = table.getSelectedRows();
            var conf = {
                component: SysAppAllMenu,
                title: this.$ti18('joinToolbarRsTitle', '同步按钮至选择菜单', 'apiFormCore'),
                curVm: this,
                widthHeight: ['400px', '550px']
            };
            Util.open(conf, function (action) {
                if (action != 'ok') {
                    return;
                }
                var menus = this.getSelectMenu();
                if (!menus || menus.length == 0) {
                    this.$message.error(this.$ti18('selMenuTips', '请选择菜单！', 'apiFormCore'));
                    return;
                }
                SysMenuApi.syncMenuBtns({
                    menuId: menus[0].id,
                    btns: JSON.stringify(btns)
                }).then(res => {
                })
            });
        },
        changeFieldName(value, record) {
            for (var i = 0; i < this.fieldDatas.length; i++) {
                if (this.fieldDatas[i].value == value) {
                    record.dataType = this.fieldDatas[i].key;
                    record.fieldLabel = this.fieldDatas[i].label;
                    break;
                }
            }
        },
        iconChange(icon, record) {
            delete record['btnIcon_name'];
            if (icon) {
                let iconJson = JSON.parse(icon);
                record['btnIcon_name'] = iconJson.icon;
            }
        },
    }
}